package cn.elwy.rcp.ui.explorer.process;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.AppContext;
import cn.elwy.rcp.ui.explorer.core.service.PackerService;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;

/**
 * 更新SVN代码进程
 * @author huangsq
 * @version 1.0, 2014-10-15
 * @since 1.0, 2014-10-15
 */
public class PackerProgress implements IRunnableWithProgress {

	private static final Logger logger = LoggerFactory.getLogger(PackerProgress.class);
	private PackerService service = AppContext.getService(PackerService.ID, PackerService.class);
	private ProjectConfig projectConfig;
	private Exception error;

	public PackerProgress(ProjectConfig projectConfig) {
		this.projectConfig = projectConfig;
	}

	public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		ProgressMonitor progressMonitor = new ProgressMonitor(monitor);
		try {
			service.run(projectConfig, progressMonitor);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			error = e;
		} finally {
			monitor.done();
		}
		if (monitor.isCanceled()) {
			throw new InterruptedException("The long running operation was cancelled");
		}
	}

	public boolean isError() {
		return error != null;
	}

	public Exception getError() {
		return error;
	}

}
